#include <stdio.h>
#include <stdlib.h>

int *a;
int n=0;

void display()
	{
	
	int i;
	
	printf("\n[");
	
	if(n==0)
		printf(" ");
	
	else
		{
		for(i=0; i<n-1; i++)
			printf("%d, ", a[i]);
		printf("%d", a[i]);
		}
	
	printf("]\n");
	
	}

void append(int no)
	{
	
	int i;
	
	a=realloc(a, (n+no)*sizeof(int));
	
	for(i=0; i<no; i++)
		{
		
		printf("\nEnter element: ");
		scanf("%d", &(a[n+i]));
		
		}
	
	n=n+no;
	
	}

void insert()
	{
	
	if(n==0)
		{
		printf("\nList is empty. Element will be inserted in position 0.\n");
		append(1);
		return;
		}
	
	int pos;
	
	int i;
	
	printf("\nEnter position: ");
	scanf("%d", &pos);
	
	if(pos>n)
		{
		printf("\nPosition entered is higher than length of list. Element will be inserted at end of list.\n");
		append(1);
		return;
		}
	
	if(pos<0)
		{
		printf("\nPosition entered is less than 0. Element will be inserted at beginning of list.\n");
		pos=0;
		}
	
	a=realloc(a, (n+1)*sizeof(int));
	
	for(i=n-1; i>=pos; i--)
		a[i+1]=a[i];
	
	printf("\nEnter element: ");
	scanf("%d", &a[i+1]);
	
	n=n+1;
	
	}

void myremove()
	{
	
	if(n==0)
		{
		printf("\nList is empty.\n");
		return;
		}
	
	int num;
	
	int i;
	
	int flag=0;
	
	printf("\nEnter element to remove: ");
	scanf("%d", &num);
	
	for(i=0; i<n; i++)
		if(a[i]==num)
			{
			flag=1;
			break;
			}
	
	if(flag==1)
		{
		for(; i<n; i++)
			a[i]=a[i+1];
		
		a=realloc(a, (n-1)*sizeof(int));
		n=n-1;
		}
	
	else
		printf("\nElement not found.\n");
	
	}

void pop()
	{
	
	if(n==0)
		{
		printf("\nList is empty.\n");
		return;
		}
	
	int pos;
	
	int i;
	
	printf("\nEnter index of element to be popped: ");
	scanf("%d", &pos);
	
	if(pos<0||pos>=n)
		{
		printf("\nInvalid position.\n");
		return;
		}
	
	printf("\nPopped element: %d\n", a[pos]);
	
	for(i=pos; i<n-1; i++)
		a[i]=a[i+1];
	
	a=realloc(a, (n-1)*sizeof(int));
	n=n-1;
	
	}

void count()
	{
	
	if(n==0)
		{
		printf("\nList is empty.\n");
		return;
		}
	
	int num;
	
	int i;
	
	int count=0;
	
	printf("\nEnter element to be counted: ");
	scanf("%d", &num);
	
	for(i=0; i<n; i++)
		if(a[i]==num)
			count++;
	
	printf("\nNumber of times %d occurred: %d\n", num, count);
	
	}

void myindex()
	{
	
	if(n==0)
		{
		printf("\nList is empty.\n");
		return;
		}
	
	int num;
	
	int i;
	
	printf("\nEnter the number whose index is to be found: ");
	scanf("%d", &num);
	
	for(i=0; i<n; i++)
		if(a[i]==num)
			{
			printf("\nElement %d found at position: %d\n", num, i);
			return;
			}
	
	printf("\nElement not found.\n");
	
	}

void reverse()
	{
	
	printf("\nReversed list:\n");
	
	if((n==0)||(n==1))
		{
		display();
		return;
		}
	
	int i, t;
	
	for(i=0; i<n/2; i++)
		{
		t=a[i];
		a[i]=a[n-1-i];
		a[n-1-i]=t;
		}
	
	display();
	
	}

void sort()
	{
	
	printf("\nSorted list:\n");
	
	if((n==0)||(n==1))
		{
		display();
		return;
		}
	
	int i, j, key;
	
	for(j=1; j<n; j++)
		{
		key=a[j];
		i=j-1;
		while((i>=0)&&(a[i]>key))
			{
			a[i+1]=a[i];
			i=i-1;
			}
		a[i+1]=key;
		}
	
	display();
	
	}

void main()
{

int choice;

int append_no;

a=malloc(0*sizeof(int));

while(1)
	{
	
	printf("\nMenu\n\n1. Display\n2. Insert\n3. Append\n4. Extend\n5. Remove\n6. Pop\n7. Count\n8. Index\n9. Reverse\n10. Sort\n11. Exit");
	printf("\n\nEnter your choice: ");
	scanf("%d", &choice);
	
	switch(choice)
		{
		
		case 1:
			display();
			break;
		
		case 2:
			insert();
			break;
		
		case 3:
			append(1);
			break;
		
		case 4:
			printf("\nEnter number of elements in list: ");
			scanf("%d", &append_no);
			append(append_no);
			break;
		
		case 5:
			myremove();
			break;
		
		case 6:
			pop();
			break;
		
		case 7:
			count();
			break;
		
		case 8:
			myindex();
			break;
		
		case 9:
			reverse();
			break;
		
		case 10:
			sort();
			break;
		
		case 11:
			printf("\n");
			exit(0);
		
		default:
			printf("\nInvalid option.\n");
		
		}
	
	}

}
